//
//  CarView.swift
//  XXIOT
//
//  Created by xx on 2024/8/23.
//

import UIKit
import FastSwiftSDK

class CarView: FastView {

    private lazy var directionView: DirectionView = {
        let view = DirectionView()
        self.addSubview(view)
        return  view
    } ()
    
    private lazy var speedView: SpeedView = {
        let view = SpeedView()
        view.typeAction(index: 1)
        self.addSubview(view)
        return  view
    } ()
    
    private lazy var btn_keepSpeed : FastButton = {
        let btn =  FastButton.init(title: "定速：关", imgName: "",color: .x333 ,font:UIFont.system(15));
        btn.backgroundColor = .xF8
        btn.tag = 0
        btn.click = { [weak self] in
            if self?.tag == 0{
                self?.tag = 1
            }else{
                self?.tag = 0
            }
            if self?.tag == 0{
                btn.titleColor = .x333
                btn.backgroundColor = .xF8
                btn.title = "定速：关"
                self?.directionView.lock = false
            }else{
                btn.titleColor = .white
                btn.backgroundColor = themeColor
                btn.title = "定速：开"
                self?.directionView.lock = true
            }
        }
        self.addSubview(btn)
        return  btn
    } ()
    
    private lazy var stateView_mq: StateView = {
        let view = StateView()
        view.lbl_title.text = "MQ :"
        self.addSubview(view)
        return  view
    } ()
    
    private lazy var stateView_bt: StateView = {
        let view = StateView()
        view.lbl_title.text = "BT :"
        self.addSubview(view)
        return  view
    } ()
   
    override func layoutSubviews() {
        super.layoutSubviews()
        directionView.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(300)
        }
        speedView.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(directionView.snp.right).offset(20)
            make.width.equalTo(100)
            make.height.equalTo(180)
        }
        btn_keepSpeed.snp.makeConstraints { make in
            make.top.equalTo(speedView.snp.bottom)
            make.left.right.equalTo(speedView)
            make.height.equalTo(60)
        }
        stateView_mq.snp.makeConstraints { make in
            make.top.equalTo(btn_keepSpeed.snp.bottom)
            make.left.right.equalTo(speedView)
            make.height.equalTo(30)
        }
        stateView_bt.snp.makeConstraints { make in
            make.top.equalTo(stateView_mq.snp.bottom)
            make.left.right.equalTo(speedView)
            make.height.equalTo(stateView_mq)
        }
    }
    
    //MARK: Action
    func setState(type:String,state:Int){
        if type == "MQ"{
            stateView_mq.setState(state)
        }
        if type == "BT"{
            stateView_bt.setState(state)
        }
    }
}
class StateView:UIView{
    lazy var lbl_title : FastLabel = {
        let lbl =  FastLabel.init("")
        self.addSubview(lbl)
        return  lbl
    } ()
    private lazy var lbl_point : FastLabel = {
        let lbl =  FastLabel.init("")
        lbl.cornerRadius = 5
        lbl.backgroundColor = .red
        self.addSubview(lbl)
        return  lbl
    } ()
    override func layoutSubviews() {
        lbl_title.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        lbl_point.snp.makeConstraints { make in
            make.left.equalTo(lbl_title.snp.right).offset(10)
            make.size.equalTo(10)
            make.centerY.equalToSuperview()
        }
    }
    func setState(_ state:Int){
        if state == 0{
            lbl_point.backgroundColor = .red
        }else{
            lbl_point.backgroundColor = .green
        }
    }
}
class SpeedView:UIView{

    private lazy var btn_low : FastButton = {
        let btn =  FastButton.init(title: "低", imgName: "",color: .x333 ,font:UIFont.system(15));
        btn.backgroundColor = .xF8
        btn.click = { [weak self] in
            self?.typeAction(index: 0)
        }
        self.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_middle : FastButton = {
        let btn =  FastButton.init(title: "中", imgName: "",color: .x333 ,font:UIFont.system(15));
        btn.backgroundColor = .xF8
        btn.click = { [weak self] in
            self?.typeAction(index: 1)
        }
        self.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_height : FastButton = {
        let btn =  FastButton.init(title: "高", imgName: "",color: .x333 ,font:UIFont.system(15));
        btn.backgroundColor = .xF8
        btn.click = { [weak self] in
            self?.typeAction(index: 2)
        }
        self.addSubview(btn)
        return  btn
    } ()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        btn_low.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.right.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(1.0/3.0)
        }
        btn_middle.snp.makeConstraints { make in
            make.top.equalTo(btn_low.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(btn_low)
        }
        btn_height.snp.makeConstraints { make in
            make.top.equalTo(btn_middle.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(btn_low)
        }
    }
    
    func typeAction(index:Int){
        btn_low.backgroundColor = .xF8
        btn_low.titleColor = .x333
        btn_middle.backgroundColor = .xF8
        btn_middle.titleColor = .x333
        btn_height.backgroundColor = .xF8
        btn_height.titleColor = .x333
        var msg = ""
        if index == 0{
            btn_low.backgroundColor = themeColor
            btn_low.titleColor = .white
            msg = CAR_SPEED_LOW
        }
        if index == 1{
            btn_middle.backgroundColor = themeColor
            btn_middle.titleColor = .white
            msg = CAR_SPEED_MIDDLE
        }
        if index == 2{
            btn_height.backgroundColor = themeColor
            btn_height.titleColor = .white
            msg = CAR_SPEED_HEIGHT
        }
        print("speed:\(msg)")
        XXBTManager.shared.sendData(data: msg)
//        XXMQTT.shared.send(topic: .CAR_SPEED, msg: msg)
    }
    
 
}
class DirectionView:UIView{
    var lock = false// 定速巡航 （前 后）
    private lazy var img_forward: UIImageView = {
        let view = UIImageView.init(image: kImage("forward"))
        view.isUserInteractionEnabled = true
        view.tag = 0
        let long = UILongPressGestureRecognizer(target: self, action:#selector(handleLongPress))
        view.addGestureRecognizer(long)
        self.addSubview(view)
        return  view
    } ()
    
    private lazy var img_back: UIImageView = {
        let view = UIImageView.init(image: kImage("back"))
        view.tag = 1
        let long = UILongPressGestureRecognizer(target: self, action:#selector(handleLongPress))
        view.addGestureRecognizer(long)
        self.addSubview(view)
        view.isUserInteractionEnabled = true
        return  view
    } ()
    
    private lazy var img_left: UIImageView = {
        let view = UIImageView.init(image: kImage("left"))
        view.tag = 2
        let long = UILongPressGestureRecognizer(target: self, action:#selector(handleLongPress))
        view.addGestureRecognizer(long)
        self.addSubview(view)
        view.isUserInteractionEnabled = true
        return  view
    } ()
    
    private lazy var img_right: UIImageView = {
        let view = UIImageView.init(image: kImage("right"))
        view.tag = 3
        let long = UILongPressGestureRecognizer(target: self, action:#selector(handleLongPress))
        view.addGestureRecognizer(long)
        self.addSubview(view)
        view.isUserInteractionEnabled = true
        return  view
    } ()
    
    private lazy var img_leftTurn: UIImageView = {
        let view = UIImageView.init(image: kImage("leftTurn"))
        view.tag = 4
        let long = UILongPressGestureRecognizer(target: self, action:#selector(handleLongPress))
        view.addGestureRecognizer(long)
        self.addSubview(view)
        view.isUserInteractionEnabled = true
        return  view
    } ()
    private lazy var img_rightTurn: UIImageView = {
        let view = UIImageView.init(image: kImage("rightTurn"))
        view.tag = 5
        let long = UILongPressGestureRecognizer(target: self, action:#selector(handleLongPress))
        view.addGestureRecognizer(long)
        self.addSubview(view)
        view.isUserInteractionEnabled = true
        return  view
    } ()
    
    private lazy var img_stop: UIImageView = {
        let view = UIImageView.init(image: kImage("stop"))
        view.tag = 10
        let long = UILongPressGestureRecognizer(target: self, action:#selector(handleLongPress))
        view.addGestureRecognizer(long)
        self.addSubview(view)
        view.isUserInteractionEnabled = true
        return  view
    } ()
    
    @objc func handleLongPress(sender:UILongPressGestureRecognizer){
        var msg = ""
        let tag = sender.view?.tag
        if sender.state == .began{
            print("began")
            if tag == 0{// 上
                msg = CAR_FORWARD
            }
            if tag == 1{// 下
                msg = CAR_BACKWARD
            }
            if tag == 2{// 左
                msg = CAR_LEFT
            }
            if tag == 3{// 右
                msg = CAR_RIGHT
            }
            if tag == 4{// 左掉头
                msg = CAR_LEFTTURN
            }
            if tag == 5{// 右掉头
                msg = CAR_RIGHTTURN
            }
            if tag == 10{// 停止
                msg = CAR_STOP
            }
            XXBTManager.shared.sendData(data: msg)
//            XXMQTT.shared.send(topic: XXTOPIC.CAR, msg: msg)
            print("tag:\(tag)")
        }else if sender.state == .ended{
            if tag == 0 || tag == 1{
                if lock == false{
                    XXBTManager.shared.sendData(data: CAR_STOP)
//                    XXMQTT.shared.send(topic: XXTOPIC.CAR, msg: CAR_STOP)
                    print("stop")
                }
            }else{
                XXBTManager.shared.sendData(data: CAR_STOP)
//                XXMQTT.shared.send(topic: XXTOPIC.CAR, msg: CAR_STOP)
                print("stop")
            }
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        img_leftTurn.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.size.equalToSuperview().multipliedBy(1.0/3.0)
        }
        
        img_rightTurn.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.size.equalToSuperview().multipliedBy(1.0/3.0)
        }
        
        img_stop.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalToSuperview().multipliedBy(1.0/3.0)
        }
        
        img_forward.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.size.equalToSuperview().multipliedBy(1.0/3.0)
        }
        img_back.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview()
            make.size.equalToSuperview().multipliedBy(1.0/3.0)
        }
        img_left.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
            make.size.equalToSuperview().multipliedBy(1.0/3.0)
        }
        img_right.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.size.equalToSuperview().multipliedBy(1.0/3.0)
        }
    }
}
